#include<bits/stdc++.h>
#define ll long long
#define L x<<1
#define R x<<1|1
#define mid (l+r>>1)
#define lc L,l,mid
#define rc R,mid+1,r
#define OK Ll<=l&&r<=Rr
#define Root 1,1,tp
#define rep(x,y,z) for(int x=(y);x<=(z);x++)
#define per(x,y,z) for(int x=(y);x>=(z);x--)
#define repn(x) rep(x,1,n)
#define repm(x) rep(x,1,m)
#define pb push_back
#define e(x) for(int i=h[x],y=to[i];i;i=nxt[i],y=to[i])
#define E(x) for(auto y:p[x])
#define Pi pair<int,int>
#define ui unsigned ll
inline int read(){int s=0,w=1;char c=getchar();while(c<48||c>57) {if(c=='-') w=-1;c=getchar();}while(c>=48&&c<=57)s=(s<<1)+(s<<3)+c-48,c=getchar();return s*w;}
inline void pf(int x){if(x<0) putchar('-'),x=-x;if(x>9)pf(x/10);putchar(x%10+48);}
using namespace std;
const int N=2e6+10,M=7e6+5,inf=(1LL<<31)-1,mod=998244353;
const ll llf=1e18;
inline void add(int &a,int b){((a+=b)>=mod) and (a-=mod);}
inline int Add(int a,int b){return add(a,b),a;}
inline int mul(int a,int b){return 1LL*a*b%mod;}
inline void Mul(int &a,int b){a=mul(a,b);}
inline void red(int &a,int b){add(a,mod-b);}
inline int Red(int a,int b){return red(a,b),a;}
inline int qp(int a,int b){if(!b)return 1;int c=qp(a,b>>1);Mul(c,c);if(b&1)Mul(c,a);return c;}
inline int INV(int x){return qp(x,mod-2);}
int n,m,k,ty,h[N],to[N],nxt[N],cnt;
inline void add_(int a,int b){
	to[++cnt]=b,nxt[cnt]=h[a],h[a]=cnt;
}
struct edge{
	int x,y;
}e[N],q[N];
int dfn[N],low[N],Time,bel[N],tot,s[N],tp;
inline void tarjan(int x,int fa){
	dfn[x]=low[x]=++Time,s[++tp]=x;
	e(x)if(!dfn[y])tarjan(y,x),low[x]=min(low[x],low[y]);
	else if(y^fa)low[x]=min(low[x],dfn[y]);
	if(low[x]==dfn[x]){
		tot++;
		for(int y=s[tp--];;y=s[tp--]){
			bel[y]=tot;
			if(x==y)break;
		}
	}
}
vector<int>p[N];
int f[N],dep[N],siz[N],son[N],top[N],Id[N];
inline void dfs(int x,int fa){
	f[x]=fa,dep[x]=dep[fa]+1,siz[x]=1;
	E(x)if(y^fa)dfs(y,x),siz[x]+=siz[y],son[x]=siz[y]>siz[son[x]]?y:son[x];
}
inline void Dfs(int x,int low){
	top[x]=low,dfn[x]=++Time,Id[Time]=x;
	if(son[x])Dfs(son[x],low);
	E(x)if(y^f[x]&&y^son[x])Dfs(y,y);
}
inline int lca(int x,int y){
	while(top[x]^top[y])dep[top[x]]>dep[top[y]]?x=f[top[x]]:y=f[top[y]];
	return dep[x]<dep[y]?x:y;
}
int c[N],ans,W;
inline void dfsR(int x,int fa){
	E(x)if(y^fa)dfsR(y,x),c[x]+=c[y];
	ans=max(ans,c[x]);
}
inline void subtaskR(){
	repm(i){
		int x=read(),y=read();
		add_(x,y),add_(y,x);
	}
	tarjan(1,0),Time=0;
	if(tot==1)return puts("0"),void();
	repn(x)e(x)if(bel[x]^bel[y])p[bel[x]].pb(bel[y]);
	dfs(1,0),Dfs(1,1);
	rep(i,1,k){
		int x=bel[read()],y=bel[read()];
		if(x!=y)c[x]++,c[y]++,c[lca(x,y)]-=2;
	}
	dfsR(1,0);
	cout <<ans;
}
inline void subtaskt(){
	repm(i){
		int x=read(),y=read();
		p[x].pb(y),p[y].pb(x);
	}
	dfs(1,0),Dfs(1,1);
	rep(i,1,k){
		int x=read(),y=read();
		if(x!=y)c[x]++,c[y]++,c[lca(x,y)]-=2;
	}
	dfsR(1,0);
	sort(c+1,c+n+1);
	cout <<c[n-1]+c[n];
}
inline int find(int x){
	return f[x]==x?x:f[x]=find(f[x]);
}
inline int dist1(int x,int t){
	e(x)if(y==t)return 1;
	return 2;
}
inline int calc1(int x,int y){
	repn(i)f[i]=i;
	repm(i)if(i!=x&&i!=y)f[find(e[i].x)]=find(e[i].y);
	repn(i)h[i]=0;cnt=0;
	int sum=0,l,r;
	l=find(e[x].x),r=find(e[x].y),add_(l,r),add_(r,l);
	l=find(e[y].x),r=find(e[y].y),add_(l,r),add_(r,l);
	rep(i,1,k){
		l=find(q[i].x),r=find(q[i].y);
		if(l^r)sum+=dist1(l,r);
	}
	return sum;
}
inline void subtask1(){
	repm(i)e[i].x=read(),e[i].y=read();
	rep(i,1,k)q[i].x=read(),q[i].y=read();
	repm(i)rep(j,i+1,m)ans=max(ans,calc1(i,j));
	cout <<ans;
}
bool inc[N],vis[N];
inline int dfsf(int x,int fr){
	vis[x]=1;
	e(x)if(i^fr^1){
		if(!vis[y]){
			int w=dfsf(y,i);
			if(w==0)continue;
			if(w==-1)return -1;
			inc[y]=1,s[++tp]=y;
			return w==x?-1:w;
		}else return inc[y]=1,s[++tp]=y,y;
	}
	return 0;
}
inline void dfsT(int x,int fa,int rt){
	f[x]=fa,dep[x]=dep[fa]+1,siz[x]=1,bel[x]=rt;
	e(x)if(y^fa&&!inc[y])dfsT(y,x,rt),siz[x]+=siz[y],son[x]=siz[y]>siz[son[x]]?y:son[x];
}
inline void DfsT(int x,int low){
	top[x]=low,dfn[x]=++Time,Id[Time]=x;
	if(son[x])DfsT(son[x],low);
	e(x)if(y^f[x]&&y^son[x]&&!inc[y])DfsT(y,y);
}
inline void dfsRT(int x,int fa){
	e(x)if(y^fa&&!inc[y])dfsRT(y,x),c[x]+=c[y];
}
struct node{
	int l,r,k;
};
vector<node>d[N];
struct seg{
	int mx,laz;
}xd[N<<2];
inline void insert(int x,int k){
	xd[x].mx+=k,xd[x].laz+=k;
}
inline void getup(int x){
	xd[x].mx=max(xd[L].mx,xd[R].mx);
}
inline void pushdown(int x){
	insert(L,xd[x].laz),insert(R,xd[x].laz),xd[x].laz=0;
}
inline void modify(int x,int l,int r,int Ll,int Rr,int k){
	if(OK)return insert(x,k),void();
	pushdown(x);
	if(Ll<=mid)modify(lc,Ll,Rr,k);
	if(Rr>mid)modify(rc,Ll,Rr,k);
	getup(x);
}
inline void sol(int l,int r,int Ll,int Rr){
	if(l>r||Ll>Rr)return;
	d[Ll].pb({l,r,1}),d[Rr+1].pb({l,r,-1});
}
inline void subtaskT(){
	cnt=1;
	repm(i){
		int x=read(),y=read();
		add_(x,y),add_(y,x);
	}
	rep(i,1,k)q[i].x=read(),q[i].y=read();
	dfsf(1,0);
	rep(i,1,tp)dfsT(s[i],0,s[i]),DfsT(s[i],s[i]);
	rep(i,1,tp)Id[s[i]]=i;
	rep(i,1,k){
		int x=q[i].x,y=q[i].y;
		if(inc[x]&&inc[y])continue;
		if(inc[y])swap(x,y);
		if(inc[x])c[y]++,c[bel[y]]--;
		else if(bel[x]==bel[y])c[x]++,c[y]++,c[lca(x,y)]-=2;
		else c[x]++,c[y]++,c[bel[x]]--,c[bel[y]]--;
	}
	rep(i,1,tp)dfsRT(s[i],0);
	sort(c+1,c+n+1);
	ans=c[n]+c[n-1];
	rep(i,1,k){
		int x=bel[q[i].x],y=bel[q[i].y];
		if(x==y)continue;
		x=Id[x],y=Id[y];
		if(x>y)swap(x,y);
		sol(1,x-1,x,y-1),sol(x,y-1,y,n);
	}
	rep(i,1,tp){
		for(auto x:d[i])modify(Root,x.l,x.r,x.k);
		ans=max(ans,xd[1].mx);
	}
	cout <<ans;
}
inline void subtaskh(){
	repm(i){
		int x=read(),y=read();
		add_(x,y),add_(y,x);
	}
	tarjan(1,0),Time=0;
	if(tot==1)return puts("0"),void();
	repn(x)e(x)if(bel[x]^bel[y])p[bel[x]].pb(bel[y]);
	dfs(1,0),Dfs(1,1);
	rep(i,1,k){
		int x=bel[read()],y=bel[read()];
		if(x!=y)c[x]++,c[y]++,c[lca(x,y)]-=2;
	}
	dfsR(1,0);
	sort(c+1,c+tot+1);
	cout <<c[tot]+c[tot-1];
}
inline void Main(){
	n=read(),m=read(),k=read(),ty=read();
	if(n==1)return puts("0"),void();
	if(ty==1)return subtaskR(),void();
	if(m==n-1)return subtaskt(),void();
	if(n<=300&&m<=300&&k<=300)return subtask1(),void();
	if(n==m)return subtaskT(),void();
	subtaskh();
}
signed main(){
	freopen("hide.in","r",stdin);
	freopen("hide.out","w",stdout);
	int T=1;
	while(T--)Main();
	return 0;
}